package common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import common.utils.wechat.MyX509TrustManager;
import net.sf.json.JSONObject;

/**
 * http请求工具类
 * @author wujun
 */
public class HttpRequestUtil {
	private static Logger log = Logger.getLogger(HttpRequestUtil.class);
	
	/* ajax请求头名称 */
	private static final String AJAX_HEADER_NAME = "x-requested-with";
	
	/**
	 * 判断ajax请求
	 */
	public static boolean isAjaxRequest(HttpServletRequest request){
		String val = request.getHeader(AJAX_HEADER_NAME);
		return val != null && !val.equals("");
	}
	
	/**
	 * 判断多功能的表单(例如上传文件)
	 * @param request
	 * @return
	 */
	public static boolean isMutipartForm(HttpServletRequest request){
		return request != null 
				&& request.getContentType() != null 
				&& request.getContentType().contains("multipart/form-data");
	}
	
	/**
	 * 判断请求Url是否指定的前缀开始
	 * @param request
	 * @param urlPrefix url指定的前缀
	 * @return
	 */
	public static boolean isUrlStartWith(HttpServletRequest request, String urlPrefix){
		return request.getServletPath().startsWith(urlPrefix);
	}
	
	/**
	 * 获取当前请求url
	 */
	public static String currrentUrl(HttpServletRequest requst){
		if(requst.getQueryString() != null && !requst.getQueryString().equals("")){
			return requst.getRequestURI() + "?" + requst.getQueryString();
		}
		return requst.getRequestURI();
	}
	
	/**
	 * 指定当前请求url按utf-8编码格式编码
	 * @param currentUrl
	 * @return
	 */
	public static String encodeUtf8(String currentUrl){
		try {
			return URLEncoder.encode(currentUrl, "utf-8");
		} catch (UnsupportedEncodingException e) {
			log.error("HttpRequestUtil encodeUtf8" +e.getMessage());
		}
		return null;
	}
	
	/**
	 * 指定url按utf-8编码格式解码
	 * @param url
	 * @return
	 */
	public static String decodeUtf8(String url) {
		try {
			return URLDecoder.decode(url, "utf8");
		} catch (UnsupportedEncodingException e) {
			log.error("HttpRequestUtil decodeUtf8" +e.getMessage());
		}
		return null;
	}
	
	/**
	 * 清除当前url的contextPath()为空字符串
	 * @param currentUrl
	 * @param request
	 * @return
	 */
	public static String removeServletContexName(String currentUrl, HttpServletRequest request){
		return currentUrl.replaceFirst(request.getServletContext().getContextPath(), "");
	}
	
	/**
	 * 读取请求以字节流的方式返回指定utf-8编号字符串
	 * @param request
	 * @return
	 */
	public static String readStream(HttpServletRequest request){
		byte[] bytes = new byte[1024 * 1024];
		try {
			InputStream is = request.getInputStream();
			int nRead = 1;
			int nTotalRead = 0;
			while (nRead > 0){
				nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
				if(nRead > 0)
					nTotalRead = nTotalRead + nRead;
			}
			return new String(bytes, 0, nTotalRead, "utf-8");	
		} catch (IOException e) {
			log.error("HttpRequestUtil readStream" +e.getMessage());
		}
		return "";
	}
	
	/**
	 * 发起https请求并获取结果
	 *
	 * @param requestUrl
	 *            请求地址
	 * @param requestMethod
	 *            请求方式（GET、POST）
	 * @param outputStr
	 *            提交的数据
	 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
	 */
	    public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();
		try {
			// 创建SSLContext对象，并使用我们指定的信任管理器初始化
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			// 设置请求方式（GET/POST）
			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod))
				httpUrlConn.connect();

			// 当有数据需要提交时
			if (null != outputStr) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				// 注意编码格式，防止中文乱码
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}

			// 将返回的输入流转换成字符串
			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			// 释放资源
			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
			// jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (ConnectException ce) {
			log.info("Weixin server connection timed out.");
		} catch (Exception e) {
			log.info("https request error:{}" + e.getMessage());
		}
		return jsonObject;
	}
}
